package com.angel.provider.sys.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.angel.core.base.BaseProviderImpl;
import com.angel.core.util.StringUtils;
import com.angel.dao.health.SysMessageEmailMapper;
import com.angel.health.SysMessageEmail;
import com.angel.provider.sys.SysManagerMessageProvider;

/**
 * 高亚林
 * 2016年9月28日下午6:04:20
 */

@Service
public class SysManagerMessageProviderImpl extends BaseProviderImpl<SysMessageEmail> implements SysManagerMessageProvider{

	@Autowired
	private SysMessageEmailMapper mapper;
	
	//批量删除
	public int delete(String ids) {
		try {
			String[] idarr = ids.split("&");
			for (String id : idarr) {
				SysMessageEmail message = new SysMessageEmail();
				message.setId(Integer.parseInt(id));
				message.setDelFlag(0);
				mapper.updateByPrimaryKeySelective(message);
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return 0;
		}
		return 1;
	}

	//删除单个用户
	public int deleteById(SysMessageEmail message) {
		message.setDelFlag(0);
		return mapper.updateByPrimaryKeySelective(message);
	}

	//保存
	public Map<String, String> save(SysMessageEmail message) {
		Map<String, String> map = new HashMap<String, String>();
		if(message == null || message.getIds() == null || message.getIds() == ""){
			map.put("msg", "目标不能为空");
			map.put("code", "0");
			return map;
		}
		message.setSendTime(new Date());
		message.setDelFlag(1);
		if(!StringUtils.isEmpty(message.getIds())){
			String[] ids = message.getIds().split(",");
			for (String id : ids) {
				message.setCustomerId(Integer.parseInt(id));
				mapper.insert(message);
			}
		}
		map.put("msg", "添加成功");
		map.put("code", "1");
		return map;
	}

	//根据用户查询
	public Map<String, Object> findUserById(SysMessageEmail message) {
		List<Map<String, Object>> list = mapper.findAll(message);
		if(list != null && list.size() > 0 ){
			return list.get(0);
		}
		return null;
	}

	//修改
	public Map<String, String> update(SysMessageEmail message) {
		Map<String, String> map = new HashMap<String, String>();
		if(message == null || message.getIds() == null || message.getIds() == ""){
			map.put("msg", "目标不能为空");
			map.put("code", "0");
			return map;
		}
		mapper.updateByPrimaryKeySelective(message);
		map.put("msg", "修改成功");
		map.put("code", "1");
		return map;
	}

}
